/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.act.utils.ActUtils;
import com.thinkgem.jeesite.modules.stock.dao.WarehouseLocDao;
import com.thinkgem.jeesite.modules.stock.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.dao.InOrderDao;
import com.thinkgem.jeesite.modules.stock.dao.InOrderDetailDao;

/**
 * 入库Service
 * @author 入库
 * @version 2019-12-21
 */
@Service
@Transactional(readOnly = true)
public class InOrderService extends CrudService<InOrderDao, InOrder> {

	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private InOrderDetailDao inOrderDetailDao;
	
	public InOrder get(String id) {
		InOrder inOrder = super.get(id);
		inOrder.setInOrderDetailList(inOrderDetailDao.findList(new InOrderDetail(inOrder)));
		return inOrder;
	}
	
	public List<InOrder> findList(InOrder inOrder) {
		return super.findList(inOrder);
	}
	
	public Page<InOrder> findPage(Page<InOrder> page, InOrder inOrder) {
		return super.findPage(page, inOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(InOrder inOrder) {
		if (StringUtils.isBlank(inOrder.getId())) {
			inOrder.preInsert();
			dao.insert(inOrder);
			// 启动流程
			actTaskService.startProcess("in_order", "s_in_order", inOrder.getId(), "入库审批");



		}
		else{
			inOrder.preUpdate();
			dao.update(inOrder);
			inOrder.getAct().setComment(("yes".equals(inOrder.getAct().getFlag())?"[重申] ":"[销毁] ")+"入库审批");

			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass", "yes".equals(inOrder.getAct().getFlag())? "1" : "0");
			actTaskService.complete(inOrder.getAct().getTaskId(), inOrder.getAct().getProcInsId(), inOrder.getAct().getComment(), "入库审批", vars);

		}
		for (InOrderDetail inOrderDetail : inOrder.getInOrderDetailList()){
			if (inOrderDetail.getId() == null){
				continue;
			}
			if (InOrderDetail.DEL_FLAG_NORMAL.equals(inOrderDetail.getDelFlag())){
				if (StringUtils.isBlank(inOrderDetail.getId())){
					inOrderDetail.setOrder(inOrder);
					inOrderDetail.preInsert();
					inOrderDetailDao.insert(inOrderDetail);
				}else{
					inOrderDetail.preUpdate();
					inOrderDetailDao.update(inOrderDetail);
				}
			}else{
				inOrderDetailDao.delete(inOrderDetail);
			}
		}

	}
	
	@Transactional(readOnly = false)
	public void delete(InOrder inOrder) {
		super.delete(inOrder);
		inOrderDetailDao.delete(new InOrderDetail(inOrder));
	}

	@Autowired
	private StockService stockService;
	@Autowired
	private ContainerService containerService;
	@Autowired
	private ContainerDetailService containerDetailService;

	@Autowired
	private StockWarehouseService stockWarehouseService;
	@Autowired
	private WarehouseLocDao warehouseLocDao;
	/**
	 * 审核审批保存
	 * @param inOrder
	 */
	@Transactional(readOnly = false)
	public void auditSave(InOrder inOrder) {

		// 设置意见
		inOrder.getAct().setComment(("yes".equals(inOrder.getAct().getFlag())?"[同意] ":"[驳回] ")+inOrder.getAct().getComment());

		inOrder.preUpdate();

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = inOrder.getAct().getTaskDefKey();

		// 审核环节
		if ("audit1".equals(taskDefKey)){

		}
		else if ("audit2".equals(taskDefKey)){
			//inOrder.setHrText(inOrder.getAct().getComment());
			dao.updateCid(inOrder);
			for (InOrderDetail inOrderDetail : inOrder.getInOrderDetailList()){

			 inOrderDetailDao.updatecid(inOrderDetail);
			}

		}
		else if ("audit3".equals(taskDefKey)){
			//inOrder.setLeadText(inOrder.getAct().getComment());
			//dao.updateLeadText(inOrder);
		}
		else if ("audit4".equals(taskDefKey)){
			for (InOrderDetail inOrderDetail : inOrder.getInOrderDetailList()){

				inOrderDetailDao.updatefactnumber(inOrderDetail);
			}
			//审核通过 入库
			if("yes".equals(inOrder.getAct().getFlag())){
				for (InOrderDetail inOrderDetail : inOrder.getInOrderDetailList()) {
					Stock s = new Stock();
					s.setW(inOrderDetail.getWoods());
					List<Stock> ss=stockService.findList(s);
                    if(ss!=null&&ss.size()>0) {
						Stock stock = ss.get(0);
						stock.setNumber(stock.getNumber() + inOrderDetail.getFactNumber());
						stockService.save(stock);
					}
					else {
						Stock stock=new Stock();
						stock.setW(inOrderDetail.getWoods());
						stock.setNumber(inOrderDetail.getFactNumber());
						stockService.save(stock);
					}




						Container container=new Container();
					    container.setC(inOrderDetail.getCid());

						 double a=0d;
						 if(inOrderDetail.getType().equals("1")){
							a=inOrderDetail.getFactNumber()/10d;
						 }else{
							 a=inOrderDetail.getFactNumber()/4d;
						 }
						BigDecimal number = new BigDecimal(a);
						int newNumber = number.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
						container.setNumber(newNumber);
						List<Container> cs=containerService.findList(container);
						if(cs!=null&&cs.size()>0){
							Container container2=cs.get(0);
							container2.setNumber(container2.getNumber()-newNumber);
							containerService.save(container2);
							ContainerDetail containerDetail=new ContainerDetail();
							containerDetail.setC(container2);
							containerDetail.setNumber(newNumber);
							containerDetail.setWnumber(inOrderDetail.getFactNumber());
							containerDetail.setWoodsid(inOrderDetail.getWoods());
							containerDetail.setName(inOrderDetail.getWoods().getName());
							containerDetailService.save(containerDetail);
							WarehouseLoc warehouseLoc=warehouseLocDao.get(container2.getC().getId());
							StockWarehouse stockWarehouse=new StockWarehouse();
							stockWarehouse.setWid(inOrderDetail.getWoods());
							stockWarehouse.setWhid(warehouseLoc.getC());
							List<StockWarehouse> sws=stockWarehouseService.findList(stockWarehouse);
							if(sws!=null&&sws.size()>0){
								stockWarehouse=sws.get(0);
								stockWarehouse.setNumber(stockWarehouse.getNumber()+inOrderDetail.getFactNumber());
								stockWarehouseService.save(stockWarehouse);
							}
							else {
								stockWarehouse.setNumber(inOrderDetail.getFactNumber());
								stockWarehouseService.save(stockWarehouse);
							}
						}




				}
			}
		}
		else if ("audit5".equals(taskDefKey)){

		}

		// 未知环节，直接返回
		else{
			return;
		}

		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass", "yes".equals(inOrder.getAct().getFlag())? "1" : "0");
		actTaskService.complete(inOrder.getAct().getTaskId(), inOrder.getAct().getProcInsId(), inOrder.getAct().getComment(), vars);

	}
}